<template>
  <div v-loading="pageLoading" class="home_league">
    <header class="game_modal_header">教练领队榜</header>
    <div class="schedule_content modal_shadow">
      <el-row class="subtotal gl_pb_15">
        <el-col :span="24">
          <el-radio-group v-model="qo.Type" size="small" @change="getPageList()">
            <el-radio-button label="Coach">教练榜</el-radio-button>
            <el-radio-button label="Leader">领队榜</el-radio-button>
          </el-radio-group>
        </el-col>
      </el-row>

      <div class="gl_pb_15">
        <el-form :model="qo" size="small" label-width="70px">
          <el-row :gutter="30">
            <el-col :span="6">
              <el-form-item label="性别">
                <el-select v-model="qo.GenderCode" style="width:100%" placeholder="请选择性别" @change="getPageList()">
                  <el-option v-for="(item, index) in genderList" :key="index" :label="item.value" :value="item.code" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="姓名">
                <el-input v-model="qo.Keyword" placeholder="请输入教练或领队姓名" @change="getPageList()" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-table :key="qo.Type" v-loading="tableLoading" :data="tableData" stripe size="small" style="width: 100%" @sort-change="onSortChange">
          <el-table-column prop="finalRank" label="排名" align="center" width="60" />
          <el-table-column prop="name" label="姓名" align="left" max-width="160">
            <template slot-scope="scope">
              <div class="agency_wrap gl_clear">
                <div class="agency_photo gl_mr_10">
                  <img :src="getStaticFileUrl(scope.row.photoUrl)" @error="errorPhoto">
                </div>
                <div style="display:inline-block;width: 80px;vertical-align: middle;">{{ scope.row.name }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="gender" label="性别" align="center" width="60" />
          <el-table-column prop="organization" label="机构名称" align="left" />

          <el-table-column width="90px" sortable="custom" label="金牌" align="center" :sort-orders="['ascending','descending', null]" :default-sort="{order: 'null'}" prop="gold">
            <template slot="header">
              <img style="padding-left:0px;vertical-align:middle;" src="@/assets/images/league/gold.png">
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.subRankList[0] ? scope.row.subRankList[0].subRankCount : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column width="90px" sortable="custom" label="银牌" align="center" :sort-orders="['ascending','descending', null]" :default-sort="{order: 'null'}" prop="silver">
            <template slot="header">
              <img style="padding-left:0px;vertical-align:middle;" src="@/assets/images/league/silver.png">
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.subRankList[1] ? scope.row.subRankList[1].subRankCount : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column width="90px" sortable="custom" label="铜牌" align="center" :sort-orders="['ascending','descending', null]" :default-sort="{order: 'null'}" prop="bronze">
            <template slot="header">
              <img style="padding-left:0px;vertical-align:middle;" src="@/assets/images/league/bronze.png">
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.subRankList[2] ? scope.row.subRankList[2].subRankCount : '' }}</span>
            </template>
          </el-table-column>

          <el-table-column label="胜率" align="center" width="80">
            <template slot-scope="scope">
              <!-- <span> {{ Number.parseInt(scope.row.winPercent * 100) }}% </span> -->
              <span>{{ scope.row.winPercent ? Math.round(scope.row.winPercent * 100) + '%' : '--' }}</span>
            </template>
          </el-table-column>

          <el-table-column width="100" prop="finalPoints" label="总分" sortable align="center" />
        </el-table>
      </div>

      <!-- 分页 -->
      <el-row style="text-align: center;">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="qo.MaxResultCount"
          :current-page="qo.Page"
          @size-change="changePageSize"
          @current-change="changePage"
        />
      </el-row>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import {
  api_GET_EventsManagementEventResultUnifiedPointRankList
} from '@/extend/api/gameswaggerApi/app';

import {
  Api_GET_EventsManagementEventResultUnifiedPointRankListQO
} from '@/lib/form/game/swaggerForm';

import defaultPhoto from '@/assets/images/face_default.png';

export default {
  props: {
    gameInfo: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data () {
    const _qo = new Api_GET_EventsManagementEventResultUnifiedPointRankListQO();
    _qo.Type = 'Coach';
    return {
      defaultPhoto: defaultPhoto, // 默认照片
      pageLoading: false,
      tableLoading: false,
      tableData: [],
      total: 0,
      qo: _qo
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict
    }),
    genderList() {
      const _gender = (this.dict && this.dict.dictGender) || [];
      return [
        {
          value: '全部',
          code: null
        },
        ..._gender
      ];
    }
  },
  watch: {
    gameInfo: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val.id) {
          this.getPageList();
        }
      }
    }
  },
  methods: {
    // 获取页面数据
    getPageList (page) {
      const _qo = JSON.parse(JSON.stringify(this.qo));
      _qo.Page = page || 1;
      this.qo.Page = _qo.Page;
      _qo.SkipCount = (_qo.Page - 1) * _qo.MaxResultCount;
      _qo.CompetitionId = this.gameInfo.id;
      _qo.IncludeFinal = true;
      _qo.IncludeSubRank = true;
      console.log('_qo: ', _qo);
      this.apiRankList(_qo);
    },
    // UnifiedPointRankList
    apiRankList(qo) {
      this.tableLoading = true;
      api_GET_EventsManagementEventResultUnifiedPointRankList(qo)
        .then(
          res => {
            this.tableLoading = false;
            this.total = res.totalCount;
            res.items.forEach((item, index) => {
              item.$order = index;
            });
            this.tableData = res.items;
            console.log(res);
          }
        )
        .catch(
          error => {
            this.tableLoading = false;
            console.log(error);
          }
        );
    },
    onSortChange({ prop, order }) {
      this.tableData.sort(this.compare(prop, order));
    },
    compare (prop, order) {
      return function (a, b) {
        let _a, _b;
        if (prop === 'gold') {
          _a = a.subRankList[0].subRankCount;
          _b = b.subRankList[0].subRankCount;
        }
        if (prop === 'silver') {
          _a = a.subRankList[1].subRankCount;
          _b = b.subRankList[1].subRankCount;
        }
        if (prop === 'bronze') {
          _a = a.subRankList[2].subRankCount;
          _b = b.subRankList[2].subRankCount;
        }

        if (order === 'ascending') {
          if (_a < _b) { return -1; }
          if (_a > _b) { return 1; }
          return 0;
        } else if (order === 'descending') {
          if (_a > _b) { return -1; }
          if (_a < _b) { return 1; }
          return 0;
        } else {
          _a = a.$order;
          _b = b.$order;
          if (_a < _b) { return -1; }
          if (_a > _b) { return 1; }
          return 0;
        }
      };
    },
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultPhoto;
      img.onerror = null;
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.qo.MaxResultCount = pagesize;
      this.getPageList();
    }
  }
};
</script>
